// 表示层
var express = require("express");
var router = express.Router();

const announcementsService = require("../service/announcementsService");

// 新增帖子
router.post("/", async function (req, res) {
    console.log(req.body);
    let { name, content } = req.body;
    let data = await announcementsService.insert({
        name,
        content,
    });
    console.log("11111",data);
    res.send(data);
});

// 查询（所有信息）
router.get("/all", async function (req, res) {
    let data = await announcementsService.findAll();
    console.log("data", data);
    res.send(data);
});

// 删除帖子
router.delete("/:id", async function (req, res) {
    let { id } = req.params;
    let data = await announcementsService.delete(id);
    res.send(data);
});

// 查询帖子
router.get("/", async function (req, res) {
    let { type, value, current, pageSize } = req.query;
    let searchParams = {};
    searchParams[type] = new RegExp(value);
    let data = await announcementsService.find(
        {
            current: current || 1,
            pageSize: pageSize,
        },
        searchParams
    );
    res.send(data);
});

// 根据id查询帖子
router.get("/:id", async function (req, res) {
    let { id } = req.params;
    let data = await announcementsService.findById(id);
    res.send(data);
});

// 修改帖子
router.put("/:id", async function (req, res) {
    let { id } = req.params;

    let { name, content } = req.body;
    let data = await announcementsService.update({
        _id: id,
        name,
        content
    });
    res.send(data);
});


module.exports = router;
